/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.misc;



import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;
import java.net.*;
import javax.net.*;

import nfse.NetFSE;

/***************************
 * 
 * Generic application class for sending data to NetFSE for indexing.
 * Currently hard-coded for Nethead data but can be used with other NetForSE.TYPE_s.
 * May be bit-rotted as this code is not used currently. Perl versions are prefered. 
 * 
 * @author 
 *
 * This code is the property of Los Alamos National Laboratory
 * and the University of California, Copyright 2005
 */
public class DataClientApp {

    /******************
     * 
     * Takes an input file path and streams it to a NetFSE server for indexing.
     * 
     * @param args - Array of parms used to set up the connection and send the data file.
     * 
     */
	public static void main(String[] args) {

        int type = -1;
        
	    /*************
		 * 
		 * TODO Must fix this code to be in sync with server implementation. See DataSocket.java.
		 * 
		 */
		try {
			long ts = System.currentTimeMillis();
			//SocketFactory sf = SSLSocketFactory.getDefault(); // Use an SSL connection
			SocketFactory sf = SocketFactory.getDefault(); // Use a plaintext connection
			Socket s = sf.createSocket(args[0], 9097);
			s.setKeepAlive(true);
			s.setSendBufferSize(NetFSE.SOCKET_SEND_SIZE);
			BufferedReader in = new BufferedReader(new FileReader(args[5]));
			PrintWriter out = new PrintWriter(s.getOutputStream());
			DataOutputStream dos = new DataOutputStream(s.getOutputStream());
			dos.writeInt(type); // Datatype is Nethead (sv3)
			dos.writeInt(1);
			dos.writeInt(1);
			dos.writeInt(1);
			dos.writeInt(1);
			dos.writeInt(Integer.parseInt(args[1]));
			dos.writeInt(Integer.parseInt(args[2]));
			dos.writeInt(Integer.parseInt(args[3]));
			dos.writeInt(Integer.parseInt(args[4]));
			
            String line = in.readLine();
			while (line != null) {
				out.write(line + "\n");
				line = in.readLine();
			}
			
            out.flush();
			out.close();
			s.close();
			System.out.println("MS=" + (System.currentTimeMillis() - ts));
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.exit(0);
	}
}
